package com.xx.secret.demo;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.AES;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xx.secret.enums.OpenEnum;
import com.xx.secret.entity.Result;
import com.xx.secret.annotation.Ignore;
import com.xx.secret.annotation.Secret;
import com.xx.secret.util.Base64Util;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @author ：wangcheng
 * @date 2023/11/13
 * 三方对接加解密 注解解耦
 * localhost:8080/demo/secret/test2 post/json
 * demo 参数 {"data":"jaG1JJ4C8zBVBeLPIMDLzw9ptF3jyeRyzC1kH0Vimqg="}
 **/
@RestController
@RequestMapping("/demo/secret")
@Secret(loanEnum = OpenEnum.XX,parseArgsFlag = true,ignoreField = @Ignore(originalParamKey = {"key","xx"}))
@Slf4j
public class SecretDemoController {

    /**
     * 优先使用方法参数注解解析 bean对象接收参数
     * @author wangcheng
     * @date 2024/03/13
     * @param loanInfo
     * @return java.lang.Object
    **/
    @RequestMapping("/test1")
    @Secret(loanEnum = OpenEnum.XX,ignoreField = @Ignore(originalParamKey = {"key","info"}
            ,decryptParamKey = {"loanInfo.imageData","idInfo.idFrontData"}))
    public Object tests1(LoanInfo loanInfo) {
        return Result.success(loanInfo);
    }

    /**
     * 使用类上默认注解参数解析 定义参数接收
     * @author wangcheng
     * @date 2024/03/13
     * @param name
     * @return java.lang.Object
    **/
    @RequestMapping("/test2")
    public Object test2(String name) {
        return Result.success(name);
    }


    @RequestMapping("/test3")
    @Secret(loanEnum = OpenEnum.PP,parseArgsFlag = true)
    public Object test3(String name,String phone) {
        return Result.success();
    }


    public static void main(String[] args) throws UnsupportedEncodingException {
        AES aes = SecureUtil.aes(Base64Util.decode("PemfsAof+FL/WS9zaxz17A=="));
        Map<String,Object> param = new HashMap<>();
        param.put("name","hello");
        param.put("id",999);
        param.put("list", Arrays.asList("a","b","c"));
        //加密
        String encode = Base64Util.encode(aes.encrypt(JSON.toJSONString(param)));
        System.out.println("aes加密参数:"+encode);
        JSONObject param1 = new JSONObject();
        param1.put("data",encode);
        System.out.println("test1请求参数"+param1);
        //解密
        System.out.println("解密"+aes.decryptStr(encode));
        System.out.println("========================================================================================");
        //=========================================================================================================
        Map<String,Object> param2 = new HashMap<>();
        param2.put("name","hello");
        String encode2 = Base64Util.encode(aes.encrypt(JSON.toJSONString(param2)));
        System.out.println("aes加密参数:"+encode2);
        JSONObject json2 = new JSONObject();
        json2.put("data",encode2);
        System.out.println("test2请求参数"+json2);
        //解密
        System.out.println("解密"+aes.decryptStr(encode2));
    }
}
